﻿using System;
using AppFiniteStateMachine;
using UnityEngine.Serialization;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using AppEventUtils;
using System.Linq;

namespace AppGameState
{
    [Serializable]
    public class GameParams : Parameter
    {
        public int attack = 3;
        public int health = 2;
        public string name = "Game_1";

        public GameObject store_objs;
        public GameObject battle_objs;

        public float max_time_for_init = 1f;
        public float max_time_for_store = 5f;
        public float max_time_for_battle = 10f;
        
        public float m_current_time = 0f;
    }
    
    public class GameStateManager : MonoBehaviour
    {
        [Header("调试模式")]
        public bool debug = false;
        
        // public FiniteStateMachineT<GameParams> fsm;
        public FiniteStateMachineBase fsm;
        public GameParams parameter;
        
        public static GameStateManager instance;

        private void Awake()
        {
            if (instance != null)
                Destroy(this);
            instance = this;
            
            fsm = new FiniteStateMachineBase();
            fsm.initState = GameStateEnum.Init.ToString();
 
            fsm.state_dc.Add(GameStateEnum.Init.ToString(), new GameInitState(this));
            
            fsm.state_dc.Add(GameStateEnum.Store.ToString(), new StoreState(this));

            fsm.state_dc.Add(GameStateEnum.Battle.ToString(), new BattleState(this));
            
            fsm.state_dc.Add(GameStateEnum.Exit.ToString(), new GameExitState(this));

            fsm.state_dc.Add(GameStateEnum.Lose.ToString(), new GameLoseState(this));
            
            fsm.state_dc.Add(GameStateEnum.Win.ToString(), new GameWinState(this));
        }

        public string get_current_state() => fsm.get_current_state();
        
        // 在第一次帧更新之前调用 Start
        void Start()
        {
            fsm.Start();
        }

        // 每帧调用一次 Update
        void Update()
        {
            fsm.Update();
        }

        public void TransitionState(object type)
        {
            fsm.TransitionState(type.ToString());
        }

        public int get_current_coins()
        {
            return BaseInfoManager.instance.current_coins;
        }

        private void OnGUI()
        {
            if (!debug)
                return;

            if (!GameController.instance.is_ready)
                return;
            
            string message = $"current_state: {get_current_state()}\n current_coins: {get_current_coins()}"; // 要显示的文本
            
            // 创建一个 GUIStyle 来设置文本样式
            GUIStyle style = new GUIStyle(GUI.skin.label);
            style.alignment = TextAnchor.MiddleCenter; // 居中对齐
            style.fontSize = 20;                // 设置字体大小
            style.normal.textColor = Color.white;     // 文本颜色

            // 计算屏幕中心下方的区域
            float x = Screen.width / 2f;
            float y = Screen.height - style.fontSize - 10;   // 距离底部的偏移量
            Rect rect = new Rect(x - 100, y - 20, 200, 40); // 矩形区域

            // 绘制文本
            GUI.Label(rect, message, style);
            
        }
    }
    
}